package com.company.model;

import com.company.model.request.CommentForm;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.springframework.util.ObjectUtils;

/**
 * 评论实体
 * Created by tu on 2017/1/1.
 */
@Entity
@Table(name = "comments")
public class Comment implements Serializable {

  private static final long serialVersionUID = -6272276535468843102L;
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  /**
   * 公司id
   */
  @Column(name = "target_id")
  private Long targetId;
  /**
   * 父评论id
   */
  @JsonIgnore
  @Column(name = "parent_id")
  private Long parentId;
  /**
   * 评论类型-0:差评 1:好评
   */
  @Column(name = "type")
  private Integer type;

  @Column(name = "content")
  private String content;
  /**
   * 赞的数量
   */
  @Column(name = "digg_count")
  private int diggCount;

  @Column(name = "create_time")
  private Date createTime;

  @Column(name = "update_time")
  @JsonIgnore
  private Date updateTime;

  @Column(name = "create_user")
  @JsonIgnore
  private String createUser;

  @Column(name = "is_delete")
  @JsonIgnore
  private int deleted;

  @Transient
  private List<CommentChild> childs;

  public Comment() {
  }

  public Comment(CommentForm info, String createUser) {
    this.createUser = createUser;
    targetId = info.getCompanyId();
    parentId = null == info.getParentId() ? 0 : info.getParentId();
    type = info.getType();
    content = info.getContent().trim();
    createTime = new Date();
    updateTime = createTime;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public Long getTargetId() {
    return targetId;
  }

  public void setTargetId(Long targetId) {
    this.targetId = targetId;
  }

  public Long getParentId() {
    return parentId;
  }

  public void setParentId(Long parentId) {
    this.parentId = parentId;
  }

  public Integer getType() {
    return type;
  }

  public void setType(Integer type) {
    this.type = type;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  public int getDiggCount() {
    return diggCount;
  }

  public void setDiggCount(int diggCount) {
    this.diggCount = diggCount;
  }

  public Date getCreateTime() {
    return createTime;
  }

  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }

  public Date getUpdateTime() {
    return updateTime;
  }

  public void setUpdateTime(Date updateTime) {
    this.updateTime = updateTime;
  }

  public String getCreateUser() {
    return createUser;
  }

  public void setCreateUser(String createUser) {
    this.createUser = createUser;
  }

  public int getDeleted() {
    return deleted;
  }

  public void setDeleted(int deleted) {
    this.deleted = deleted;
  }

  public List<CommentChild> getChilds() {
    return childs;
  }

  public void setChilds(List<CommentChild> childs) {
    this.childs = childs;
  }

  public void setChildsByComment(List<Comment> childs) {
    if (!ObjectUtils.isEmpty(childs)) {
      List<CommentChild> cc = new ArrayList<>();
      for (Comment comment : childs
          ) {
        cc.add(new CommentChild(comment));
      }
      this.childs = cc;
    }
  }

  @Override
  public String toString() {
    return "Comment{" +
        "id=" + id +
        ", targetId=" + targetId +
        ", parentId=" + parentId +
        ", type=" + type +
        ", content='" + content + '\'' +
        ", diggCount=" + diggCount +
        ", createTime=" + createTime +
        ", updateTime=" + updateTime +
        ", createUser='" + createUser + '\'' +
        ", deleted=" + deleted +
        ", childs=" + childs +
        '}';
  }
}
